package com.littleHan.brushQuestions.web.userCenter.controller;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedToDeleteException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.exception.FailedToUpdateException;
import com.littleHan.brushQuestions.exception.FailedToUploadException;
import com.littleHan.brushQuestions.utils.GetTokenInfoUtil;
import com.littleHan.brushQuestions.web.specificTopic.pojo.vo.TopicPageVo;
import com.littleHan.brushQuestions.web.start.pojo.Comment;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.pojo.Permission;
import com.littleHan.brushQuestions.web.start.pojo.TopicCollect;
import com.littleHan.brushQuestions.web.userCenter.pojo.Notice;
import com.littleHan.brushQuestions.web.userCenter.pojo.Pagination;
import com.littleHan.brushQuestions.web.userCenter.service.UserCenterService;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * @author littleHan
 */
@RestController
@RequestMapping("/userCenter")
public class UserCenterController {

    @Autowired
    private UserCenterService userCenterService;

    @RequestMapping(value = "/getUserCM", method = RequestMethod.POST)
    public String getUserCenterMenu(HttpServletRequest request) {
        String userId = GetTokenInfoUtil.getUserId(request);
        return null;
    }

    /**
     * 个人中心-用户获取导航栏
     *
     * @param request
     * @return 是否加载导航栏成功
     */
    @RequestMapping(value = "/getNavigationBarJ", method = RequestMethod.POST)
    public String getNavigationBar(HttpServletRequest request) throws FailedToLoadException {
        String userId = GetTokenInfoUtil.getUserId(request);
        List<Permission> list = userCenterService.getNavigationBar(userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载导航栏成功").setData(list));
    }

    /**
     * 获取个人资料
     *
     * @param request
     * @return 是否加载个人资料成功
     */
    @RequestMapping(value = "/getMyInfoJ", method = RequestMethod.POST)
    public String getMyInfo(HttpServletRequest request) throws FailedToLoadException, FailedToUploadException {
        String userId = GetTokenInfoUtil.getUserId(request);
        GeneralUser generalUser = userCenterService.getMyInfo(userId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载个人资料成功").setData(generalUser));
    }

    /**
     * 修改个人资料
     *
     * @param request
     * @param generalUser 普通用户实例
     * @return 是否修改个人资料成功
     * @throws FailedToLoadException
     */
    @RequestMapping(value = "/editUserInfoJ", method = RequestMethod.POST)
    public String editUserInfo(HttpServletRequest request, @RequestBody GeneralUser generalUser) throws FailedToLoadException {
        String userId = GetTokenInfoUtil.getUserId(request);
        userCenterService.editUserInfo(userId, generalUser);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改资料成功"));
    }

    /**
     * 上传头像
     * @param file 用户上传的头像文件
     * @param request
     */
    @PostMapping("/uploadIcon")
    public String uploadIcon(@RequestBody MultipartFile file,HttpServletRequest request) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, FailedToUpdateException {
        System.out.println(GetTokenInfoUtil.getUserId(request));
        String iconUrl=userCenterService.uploadIcon(GetTokenInfoUtil.getUserId(request),file);
        return JSON.toJSONString(new Result().setCode(200).setMessage("上传头像成功").setData(iconUrl));
    }

    /**
     * 根据用户id，加载收藏的题目
     *
     * @param request    请求对象
     * @param pagination 页码数对象
     * @return 是否成功获取收藏题目列表成功
     */
    @RequestMapping(value = "/getUserCollectTopicListJ", method = RequestMethod.POST)
    public String getUserCollectTopicList(HttpServletRequest request, @RequestBody Pagination pagination) throws FailedToLoadException {
        Map<String, Object> map = userCenterService.getUserCollectTopicList(GetTokenInfoUtil.getUserId(request), pagination);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取收藏的题目成功").setData(map));
    }

    /**
     * 用户取消收藏题目，根据收藏的id号，删除tbl_topic_collect中的记录
     *
     * @param topicCollect 收藏题目对象，里面只有id有值
     * @return 是否成功取消收藏题目
     */
    @RequestMapping(value = "/cancelTopicJ", method = RequestMethod.POST)
    public String cancelTopic(@RequestBody TopicCollect topicCollect) throws FailedToDeleteException {
        userCenterService.cancelTopicByCollectId(topicCollect.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("取消藏题目成功"));
    }

    /**
     * 获取当前用户的所有题目，以及评论的题目，还有用户总评论数量
     *
     * @param pagination 分页实例
     * @param request    请求对象
     * @return 是否成功获取用户评论信息
     * @throws FailedToLoadException
     */
    @PostMapping("/getUserAllCommentJ")
    public String getUserAllComment(@RequestBody Pagination pagination, HttpServletRequest request) throws FailedToLoadException {
        Map<String, Object> map = userCenterService.getUserAllComment(pagination.getPageNo(), pagination.getPageSize(), GetTokenInfoUtil.getUserId(request));
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取用户评论成功").setData(map));
    }

    /**
     * 删除当前用户的评论
     *
     * @param comment 存有评论id的评论实例
     * @return
     * @throws FailedToDeleteException
     */
    @PostMapping("deleteMyCommentJ")
    public String deleteMyComment(@RequestBody Comment comment) throws FailedToDeleteException {
        userCenterService.deleteMyComment(comment.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除我的评论成功"));
    }

    /**
     * 获取用户未读的消息通知
     *
     * @param pagination 分页对象
     * @param request    请求对象
     * @return
     */
    @PostMapping("/getUserNoticeListJ")
    public String getUserNotice(@RequestBody Pagination pagination, HttpServletRequest request) {
        Map<String, Object> map = userCenterService.getUserNotice(pagination, GetTokenInfoUtil.getUserId(request));
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取消息通知成功").setData(map));

    }

    /**
     * 将消息通知设置为已读，根据通知id
     *
     * @param notice 消息通知实例，携带消息通知id
     * @return
     * @throws FailedToUpdateException
     */
    @PostMapping("/setNoticeIsCheckJ")
    public String setNoticeIsCheck(@RequestBody Notice notice) throws FailedToUpdateException {
        userCenterService.setNoticeIsCheck(notice.getId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("已读成功"));
    }
}
